﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data;
using VQWEB.common;
using System.Data.Linq;
namespace VQWEB.administrator.sources
{
    public class IntroductionData
    {
        
        public IntroductionData() { }
        //AddEdit Introduct
        public int InsertOrUpdateIntroduction(core_introduction obj)
        {
            try
            {
                int Id = 0;
                using (CoreModelDataContext context = new CoreModelDataContext())
                {
                    if (obj.introduction_id > 0)
                    {
                        core_introduction flag = GetIntroductionById(obj.introduction_id);                        
                        if (flag != null)
                        {
                            context.core_introductions.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.introduction_id;
                        }
                    }
                    else
                    {
                        context.core_introductions.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.introduction_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        }
        //View
        public core_introduction GetIntroductionById(int id) { 
            using(CoreModelDataContext context = new CoreModelDataContext()){
                var Obj = context.core_introductions.Where(i => i.introduction_id == id).SingleOrDefault();
                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        //LockUnlock
        public void LockUnlockIntro(List<int> array_id) {
        
            using(CoreModelDataContext context =new CoreModelDataContext()){
                var query = from i in context.core_introductions
                            where array_id.Contains(i.introduction_id)
                            select i;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.introduction_active = (item.introduction_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }
            }
        }
        //Delete
        public void DeleteIntro(List<int> array_id) {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from i in context.core_introductions
                            where array_id.Contains(i.introduction_id)
                            select i;
                if (query.Any())
                {
                    context.core_introductions.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
            }
        }
        //View List
        public DataTable ViewIntroduction() {
            using(CoreModelDataContext context = new CoreModelDataContext()){
                var query = from i in context.core_introductions
                            select new { 
                                introduction_id = i.introduction_id,
                                introduction_title =i.introduction_title,
                                introduction_content= i.introduction_content,
                                introduction_hit = i.introduction_hit,
                                introduction_date = i.introduction_date,
                                introduction_active = i.introduction_active
                            };
                if (query.Any())
                    return HelperData.LINQToDataTable(query);
            return new DataTable();
            }
        }
    }
}